<template>
<div>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>注册</span>
    </div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" placeholder="请输入密码" show-password></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input placeholder="请输入手机号码" v-model="ruleForm.phone" ></el-input>
      </el-form-item>
        <el-button :plain="true" type="primary" @click="submitForm('ruleForm')">注册</el-button>
    </el-form>
  </el-card>
</div>
</template>

<script>
export default {
  data(){
    return{
      ruleForm:{username:"",nickname:'',password:"",phone:""},
      rules: {
        username: [
          {required: true, message: '请输入输入用户名', trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: '请输入输入昵称', trigger: 'blur'}
        ],
        password:[
          {required: true, message: '请输入输入密码', trigger: 'blur'},
          {pattern: /(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*()_.]+)$)^[\w~!@#$%^&*()_.]{8,16}$/,
            message: '密码应为字母，数字，特殊符号，两种及以上组合，8-16位', trigger: 'blur'}
        ],
        phone:[
          {required: true, message: '请输入输入手机号码', trigger: 'blur'},
          {pattern: /^1[3-9]\d{9}$/, message: '请输入11位手机号码', trigger: 'blur'}
        ]
      }
    }
  },
  methods:{
    submitForm(formName) {
          let url = 'http://localhost:9580/users/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '注册成功！',
                type: 'success'
              });
              this.$router.push('/');
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
    }
  }
}
</script>

<style>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}
.box-card {
  width: 480px;
  /*height: px;*/
  margin: 200px auto;
  text-align: center;
}
.el-input{
  width: 360px;
  margin-bottom: 30px;
}

</style>